<!--
 * @Description: 部门管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="118"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :has-edit="false"
        :has-save="false"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
      <!-- <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">部门名称:</div>
          <div class="input">
            <el-input v-model="departmentName" placeholder="请输入部门名称" clearable />
          </div>
        </div>
      </SearchBox> -->
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="88%"
        class="tableData"
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="departmentName" label="部门名称" />
        <el-table-column prop="contact" label="负责人" />
        <el-table-column prop="phoneNum" label="联系电话" />
        <el-table-column prop="departmentDescription" label="描述" />

        <el-table-column width="120" label="操作">
          <template v-if="scope.row.departmentNo" slot-scope="scope">
            <el-button
              v-if="judgePermission(118, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="edit"
            >
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button
              v-if="judgePermission(118, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(scope.row.departmentNo)"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog :title="titleText + '部门信息'" :visible.sync="dialogFormVisible">
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="部门名称:" prop="departmentName" class="w100p">
            <el-input
              v-model="formData.departmentName"
              placeholder="请输入部门名称"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
          <el-form-item label="负责人:" prop="contact" class="w100p">
            <el-input
              v-model="formData.contact"
              placeholder="请输入负责人"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
          <el-form-item label="联系电话:" prop="phoneNum" class="w100p">
            <el-input
              v-model="formData.phoneNum"
              placeholder="请输入联系电话"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
          <el-form-item label="描述:" prop="departmentDescription" class="w100p">
            <el-input
              v-model="formData.departmentDescription"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              :disabled="!canInput"
              clearable
            />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">取 消</el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >保 存</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import departmentManage from "@/api/basic-info-manage/department-manage";
// import staffManage from "@/api/digital-operation/personnel-cost-mansge/staff-manage/index";
// import BaseMainSlot from "@/components/BaseMainSlot/basezuoyou.vue";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from "vuex";
export default {
  name: "DepartmentManage",
  components: { SearchForm, ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '部门名称',
          prop: 'departmentName',
          type: 'input',
        },
      ],
      searchlist: {},
      staffData: [],
      departmentName: "",
      rules: {
        departmentName: [
          { required: true, message: "请输入部门名称", trigger: "blur" }
        ]
        // contact: [{ required: true, message: '请选择负责人', trigger: 'change' }],
        // phoneNum: [
        //   { required: true, message: '请输入联系电话', trigger: 'blur' },
        //   {
        //     pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
        //     message: "请输入合法手机号/电话号",
        //     trigger: "blur"
        //   }]
      }
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  created() {
    // this.getStaffList();
  },
  mounted() {
    //  表格数据
    this.getTableDataList();
  },
  methods: {
    // 获取表格数据
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          departmentName: this.searchlist.departmentName
        }
      };
      departmentManage.getDepartmentList(params).then(res => {
        this.loading = false;
        let data = res.data;
        if (data) {
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    // getStaffList() {
    //   staffManage.getStaffListByCondition().then(res => {
    //     let data = res.data;
    //     this.staffData = data;
    //     // console.log(this.ipmentType);
    //   });
    // },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      // console.log(row);
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));

      // this.addActive = true;
      // this.addActive = false;
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      // console.log(this.nowChanceStau)
      // if (this.nowChanceStau !== 'add' || this.nowChanceStau !== 'edit') {
      //   this.canInput = false;
      // }
    },
    handleSelectionChange(list) {
      this.departmentNoList = [];
      this.departmentNoList = list.map(item => item.departmentNo);
      if (this.departmentNoList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    selectRow(val) {
      this.selectlistRow = val;
    },
    delmessage(formData) {
      departmentManage.delDepartmentInfo(this.departmentNoList).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.getTableDataList();
        } else {
          this.$message.warning(res.msg || "删除失败");
        }
      });
    },
    handleDelete(e) {
      // departmentManage.delInfo(e).then(res => {
      //   if (res.code === 1) {
      //     this.$message.success(res.msg);
      //     this.getTableDataList();
      //   } else {
      //     this.$message.warning(res.msg || "删除失败");
      //   }
      // });
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          departmentManage.delInfo(e).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.getTableDataList();
            } else {
              this.$message.warning(res.msg || "删除失败");
            }
          });
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            // 巡检点数组赋值进formData
            // console.log(this.formData);
            departmentManage.editDepartmentInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            // 新增接口
            this.formData.organizationNo = sessionStorage.organizationNo;
            departmentManage.addDepartmentInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$messages.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";
.buttons {
  background-color: rgba(255, 255, 255, 0);
  border-color: $inputBorderColor;
  color: $appMainTextColor;
}
.buttonBox {
  margin-bottom: 10px;
  display: flex;
  justify-content: space-around;
}
.el-form {
  /*.*/
  .el-table {
    width: 200px !important;
    // .el-table__header{
    //   width: 200px;
    // }
  }
}
</style>
